from datetime import date
from openpyxl import Workbook
from openpyxl.chart import (
    LineChart,  # 线图
    Reference,
)
from openpyxl.chart.axis import DateAxis  # 时间轴

# 创建工作表
wb = Workbook()
ws = wb.active

# 创建数据
rows = [
    ['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
    [date(2015, 9, 1), 40, 30, 25],
    [date(2015, 9, 2), 40, 25, 30],
    [date(2015, 9, 3), 50, 30, 45],
    [date(2015, 9, 4), 30, 25, 40],
    [date(2015, 9, 5), 25, 35, 30],
    [date(2015, 9, 6), 20, 40, 35],
]

# 向工作表中添加数据
for row in rows:
    ws.append(row)

# 创建线图
c1 = LineChart()
c1.title = "折线图"
c1.style = 13
c1.y_axis.title = 'y轴'
c1.x_axis.title = 'x轴'

# 地区第2-4列,第1-7行数据
data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
# 添加数据
c1.add_data(data, titles_from_data=True)

# Style the lines
# 折线图样式1
s1 = c1.series[0]  # 总共有3列数据
s1.marker.symbol = "triangle"  # 标志,三角形
s1.marker.graphicalProperties.solidFill = "FF0000"  # Marker filling 三角形线
s1.marker.graphicalProperties.line.solidFill = "FF0000"  # Marker outline
s1.graphicalProperties.line.noFill = True

# 折线图样式2
s2 = c1.series[1]
s2.graphicalProperties.line.solidFill = "00AAAA"
s2.graphicalProperties.line.dashStyle = "sysDot"  # 点线
s2.graphicalProperties.line.width = 100050  # width in EMUs

# 折线图样式3
s2 = c1.series[2]
s2.smooth = True  # Make the line smooth 平滑曲线
ws.add_chart(c1, "A10")
wb.save("line1.xlsx")
